package com.jnc.core.base.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jnc.common.base.BaseResp;
import com.jnc.common.constant.SysConstant;
import com.jnc.core.base.model.User;
import com.jnc.core.config.shiro.util.ShiroUtil;
import com.jnc.core.web.page.PageModel;
import com.jnc.core.web.page.TableResult;
import com.jnc.core.web.page.TableSupport;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Auther: jjn
 * @Date: 2018/8/11
 * @Desc: 基础Controller类
 */
@SuppressWarnings("unchecked")
public class BaseController {

    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * 初始化请求分页数据
     */
    protected void initPage(){
        PageModel model = TableSupport.buildPageRequest();
        Integer pageNum = model.getPageNum();
        Integer pageSize = model.getPageSize();
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize, model.getOrderBy());
        }
    }

    /**
     * 响应表格数据
     * @param list
     * @return
     */
    @SuppressWarnings("unchecked")
    protected TableResult getTableData(List<?> list){
        TableResult result = new TableResult();
        result.setCode(SysConstant.STATUS_CODE_OK).setRows(list).setTotal(new PageInfo(list).getTotal());
        return result;
    }

    public BaseResp success(){
        BaseResp result = new BaseResp();
        result.setCode(SysConstant.STATUS_CODE_OK).setMsg("操作成功");
        return result;
    }

    public BaseResp success(String msg){
        BaseResp result = new BaseResp();
        result.setCode(SysConstant.STATUS_CODE_OK).setMsg(msg);
        return result;
    }

    public BaseResp success(String msg, Object data){
        BaseResp result = new BaseResp();
        result.setCode(SysConstant.STATUS_CODE_OK).setMsg(msg).setData(data);
        return result;
    }

    public BaseResp error(){
        BaseResp result = new BaseResp();
        result.setCode(SysConstant.STATUS_CODE_ERROR).setMsg("操作失败");
        return result;
    }

    public BaseResp error(String msg){
        BaseResp result = new BaseResp();
        result.setCode(SysConstant.STATUS_CODE_ERROR).setMsg(msg);
        return result;
    }

    public BaseResp error(Integer code, String msg){
        BaseResp result = new BaseResp();
        result.setCode(code).setMsg(msg);
        return result;
    }

    public BaseResp error(String msg, Object data){
        BaseResp result = new BaseResp();
        result.setCode(SysConstant.STATUS_CODE_ERROR).setMsg(msg).setData(data);
        return result;
    }

    public BaseResp toAjax(int rows){
        return rows > 0 ? success() : error();
    }

    public User getUser(){
        return ShiroUtil.getUser();
    }

    public String getLoginName(){ return ShiroUtil.getLoginName(); }
}
